package gr.atc.epart;

import gr.atc.epart.CustomCategoriesManager.CustomTabData;
import gr.atc.epart.actions.Action;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.ContentUris;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class CategoryMgrActivity extends ListActivity {

	private static final int EDIT_CATEGORY = 1;
	private static final int DELETE_CATEGORY = 2;
	private static final int ADD_CATEGORY_CODE = 3;
	private static final int EDIT_CATEGORY_CODE = 4;
	ArrayList<String> categories = new ArrayList<String>();;
	String selectedCategory;
	CustomCategoriesManager categoryManager;
	CategoriesAdapter adapter;

	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.categorymgr);

		Button newCategoryButton = (Button) findViewById(R.id.newcategorybutton);
		//newCategoryButton.setBackgroundResource(android.R.drawable.ic_menu_add);

		newCategoryButton.setOnClickListener(newCategoryListener);
		categoryManager = new CustomCategoriesManager(this);

		updateCategories();

		adapter = new CategoriesAdapter(this.getBaseContext(), categories);
		setListAdapter(adapter);
		adapter.notifyDataSetChanged();
		
		adapter.notifyDataSetChanged();

		ListView list = getListView();
		registerForContextMenu(list);

	}

	protected void updateCategories() {

		int numberOfCategories = categoryManager.getCustomTabsNumber();
		categories.clear();

		for (int i = 0; i < numberOfCategories; i++) {
			CustomTabData tabData = categoryManager.getCustomTabData(i);
			categories.add(tabData.getTabName());
		}
	

	}

	public void onBackPressed() {
	
		ProgressDialog progressDialog = new ProgressDialog(this);
		progressDialog.setMessage("Please wait...");
		progressDialog.setIndeterminate(true);
		progressDialog.setCancelable(false);
		progressDialog.show();
	
		
		setResult(200);
		super.onBackPressed();

	}

	public void onRestart() {

		super.onRestart();
		updateCategories();
		adapter.notifyDataSetChanged();
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (resultCode) {

		case 200:
			updateCategories();
			adapter.notifyDataSetChanged();
			break;

		default:
			break;
		}
	}

	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {

		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
		CategoryView categoryView = (CategoryView) info.targetView;
		selectedCategory = categoryView.getName();

		menu.setHeaderTitle(selectedCategory);
		menu.add(Menu.NONE, EDIT_CATEGORY, Menu.NONE, "Edit Category");
		menu.add(Menu.NONE, DELETE_CATEGORY, Menu.NONE, "Delete Category");

	}

	public boolean onContextItemSelected(MenuItem item) {

		switch (item.getItemId()) {

		case EDIT_CATEGORY:

			CategoryVolatileDataStore categoryVolatileStore = CategoryVolatileDataStore
					.getInstance();
			categoryVolatileStore.setName(selectedCategory);

			Intent editCategoryIntent = new Intent();
			editCategoryIntent.setClassName("gr.atc.epart",
					"gr.atc.epart.EditCategoryActivity");

			startActivityForResult(editCategoryIntent, EDIT_CATEGORY_CODE);

			return true;

		case DELETE_CATEGORY:

			categoryManager.removeCustomTab(selectedCategory);
			createAlert("Category Deleted Successfully");
		default:
			return true;
		}
	}

	private OnClickListener newCategoryListener = new OnClickListener() {
		public void onClick(View v) {

			Intent editCategoryIntent = new Intent();
			editCategoryIntent.setClassName("gr.atc.epart",
					"gr.atc.epart.AddCategoryActivity");
			startActivity(editCategoryIntent);
		}

	};

	public void createAlert(String message) {

		AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
		dlgAlert.setMessage(message);
		dlgAlert.setTitle(selectedCategory);
		dlgAlert.setPositiveButton("OK", null);
		dlgAlert.setCancelable(true);
		dlgAlert.create();
		dlgAlert.show();

		updateCategories();
		adapter.notifyDataSetChanged();
		

	}
}
